package mtm.ui.misc;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ComboBoxEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.text.JTextComponent;

public class EditableComboBox extends JComboBox {
	
	private ComboBoxEditor editor;
	
	/*
	 * Constructor
	 */
	public EditableComboBox(Object[] data) {
		super(data);
		// editable combobox
		setEditable(true);
		// get editor of this comobox
		editor = getEditor();
	}
	
	
	/**
	 * Check if display text of this combo box is an integer
	 * @return
	 */
	public boolean isIntegerInput() {
		try {
			int value = Integer.parseInt(getText());
			if (value < 0)
				return false;
		} catch (NumberFormatException ex) {
			return false;
		}
		
		return true;
	}
	
	
	/*
	 * Retrieve selected text 
	 */
	public String getText() {
		return (String) (editor.getItem());
	}
	
	
	/**
	 * Set text for this combobox
	 * @param text
	 */
	public void setText(String text) {
		editor.setItem(text);
	}
}
